//
//  UIViewController+pay.m
//  CloudTest
//
//  Created by 张帅 on 2020/7/30.
//  Copyright © 2020 张帅. All rights reserved.
//

#import "UIViewController+pay.h"
#import "PayModel.h"
#import "PaymentChoiceView.h"
#import <objc/runtime.h>
#import "WXApiManager.h"
static const void *UtilityKey = &UtilityKey;


@implementation UIViewController (pay)
//https://blog.csdn.net/sz_vcp2007/article/details/51280163  在category中使用block
@dynamic pay_block;

- (PayBlock)pay_block {
    return objc_getAssociatedObject(self, UtilityKey);
}

- (void)setPay_block:(PayBlock)pay_block {
    objc_setAssociatedObject(self, UtilityKey, pay_block, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

#pragma mark 添加支付监听
/// 添加支付监听
/// @param orderId 订单id
- (void)add_pay_observer_orderId:(NSString *)orderId PayBlock:(void(^)(NSString *state))payBlock {
    self.pay_block = payBlock;
     [PaymentChoiceView showClicked:^(int type) {//支付弹窗
          if (type == 0) {
                [self aliPay_Create_Order];//支付宝
          } else {
              [self weiXin_Create_Order]; //微信
          }
      }];
        [PayModel sharedInstance].orderId = orderId;
    @weakify(self);
       [[[NSNotificationCenter defaultCenter] rac_addObserverForName:Pay_back_notice object:nil] subscribeNext:^(NSNotification * _Nullable x) {
           @strongify(self);
           if ([PayModel sharedInstance].is_pay) {
               return;
           }
         [PayModel validation_order_isPay_orderId:[PayModel sharedInstance].orderId success:^(id obj) {
               PayModel *model =obj;
               NSString * state = @"支付失败";
               if (model.is_pay) {
                   state = @"支付成功";
               }
                NSLog(@"支付状态 =%@",state);
                [self pay_results_state:state];
           } error:^{
               
           }];
       }];
       
}
-(void)pay_results_state:(NSString *)state {
    if ([state isEqualToString:@"支付成功"] && [PayModel sharedInstance].is_pay == NO) {
        [PayModel sharedInstance].is_pay = YES;
        if ([PayModel sharedInstance].is_pay) {
              if (self.pay_block) {
                self.pay_block(state);
              }
        }
    }
    
}
#pragma mark 微信创建订单
- (void)weiXin_Create_Order {
     [PayModel sharedInstance].is_pay = NO;
    
    if ([AppTools is_installation_Wei_xin]==NO) {
        return;
    }
    //订单id
    NSString * orderId=[PayModel sharedInstance].orderId ;
    [TestManageTool weiXin_Create_Order_orderId:orderId success:^(id obj) {
        PayModel * model = (PayModel *)obj;
        NSDictionary * dic = [model yy_modelToJSONObject];
        [[WXApiManager sharedManager] payUsingData:dic];//调起微信并支付
    } error:^{
        
    }];
}
#pragma mark 支付宝创建订单
- (void)aliPay_Create_Order {
//    NSString * orderNo = [TestManageTool sharedInstance].orderModel.orderNo;
//     NSString * orderNo =[PayModel sharedInstance].orderNo;
//
//    [PayModel moni_paly_orderNo:orderNo success:^(id obj) {//模拟支付
//        if (self.pay_block) {
//            self.pay_block(@"支付成功");
//        }
//    } error:^{
//        if (self.pay_block) {
//            self.pay_block(@"支付失败");
//        }
//    }];
    [PayModel sharedInstance].is_pay = NO;
//    支付宝
    NSString * orderId=[PayModel sharedInstance].orderId ;
    [TestManageTool aliPay_Create_Order_orderId:orderId success:^(id obj) {
        PayModel * model =obj;
        [AppTools payUseAliPay:model.orderData andResultCallBack:^(int type) {//网页支付或者 sdk 回调
            NSString * state = @"支付失败";
            if (type==1) {
                state = @"支付成功";
            }
            [self pay_results_state:state];
        }];
    } error:^{

    }];
}
@end
